package com.xtecher.app.user.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.xtecher.app.R;
import com.xtecher.app.column.ui.ColumnListActivity;
import com.xtecher.app.user.bean.FollowUserResponse;

import java.util.List;

/**
 * Created by Luhao on 2017/2/24.
 */

public class FollowAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<FollowUserResponse> responses;

    public FollowAdapter(Context context, List<FollowUserResponse> responses) {
        this.context = context;
        this.responses = responses;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FollowHolder(LayoutInflater.from(context).inflate(R.layout.item_follow, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FollowHolder followHolder = (FollowHolder) holder;
        FollowUserResponse userResponse = responses.get(position);
        followHolder.tvName.setText(userResponse.getName());
        Glide.with(context)
                .load(userResponse.getAvatar())
                .asBitmap()
                .placeholder(R.drawable.list_avatar)
                .error(R.drawable.list_avatar)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(new BitmapImageViewTarget(followHolder.imgAvatar) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        followHolder.imgAvatar.setImageDrawable(circularBitmapDrawable);
                    }
                });
        followHolder.cardFollow.setOnClickListener(v -> {
            Intent intent = new Intent(context, ColumnListActivity.class);
            intent.putExtra("user", userResponse);
            context.startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return responses != null ? responses.size() : 0;
    }

    private class FollowHolder extends RecyclerView.ViewHolder {

        private CardView cardFollow;
        private ImageView imgAvatar;
        private TextView tvName;

        public FollowHolder(View view) {
            super(view);
            cardFollow = (CardView) view.findViewById(R.id.card_follow);
            imgAvatar = (ImageView) view.findViewById(R.id.img_avatar);
            tvName = (TextView) view.findViewById(R.id.tv_name);

        }
    }
}
